package com.zbest.zpay.acctrans.component;

import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

import com.zbest.zpay.acctrans.dal.mapper.AccountInfMapper;
import com.zbest.zpay.acctrans.dal.model.AccountInf;
import com.zbest.zpay.acctrans.dal.model.TransJnl;
import com.zbest.zpay.acctrans.enums.ActAttrFlag;
import com.zbest.zpay.acctrans.enums.MainActSts;
import com.zbest.zpay.acctrans.enums.MainActStsW;
import com.zbest.zpay.acctrans.enums.UpdBalFlg;

public class OpenMainAccountComponent {
    private static final Logger logger = LoggerFactory.getLogger(OpenMainAccountComponent.class);

    @Autowired
    private AccountInfMapper    accountInfMapper;

    public void execute(TransJnl transJnl) {
        logger.info("开户流程-->>>");
        String drActAttr = transJnl.getDrActAttr();
        if (StringUtils.equals(drActAttr, ActAttrFlag.ACCOUNT_ATTR_PER.getCode())
            || StringUtils.equals(drActAttr, ActAttrFlag.ACCOUNT_ATTR_MER.getCode())) {
            logger.info("开户流程DR-->>>" + transJnl.getDrUsrId());
            String usrId = transJnl.getDrUsrId();
            String actTyp = transJnl.getDrActTyp();
            AccountInf retAccountInf = accountInfMapper.qryAccountInf(transJnl.getDrActNo());
            if (retAccountInf != null) {
                logger.info("账户[" + transJnl.getDrActNo() + "]已存在,返回开户成功");
            } else {
                AccountInf accountInf = new AccountInf();
                accountInf.setActNo(transJnl.getDrActNo());
                accountInf.setUsrId(usrId);
                accountInf.setActTyp(actTyp);
                accountInf.setActSts(MainActSts.MAIN_NORMAL_STS.getCode());
                accountInf.setActStsw(MainActStsW.MAIN_STSW_TTTTT.getCode());
                accountInf.setUpdBalFlg(UpdBalFlg.IS_UPDATE.getCode());
                accountInfMapper.insert(accountInf);
                logger.info("开户流程成功" + usrId);
            }
        }

        String crActAttr = transJnl.getCrActAttr();
        if (StringUtils.equals(crActAttr, ActAttrFlag.ACCOUNT_ATTR_PER.getCode())
            || StringUtils.equals(crActAttr, ActAttrFlag.ACCOUNT_ATTR_MER.getCode())) {
            logger.info("开户流程CR-->>>" + transJnl.getCrUsrId());
            String usrId = transJnl.getCrUsrId();
            String actTyp = transJnl.getCrActTyp();
            AccountInf retAccountInf = accountInfMapper.qryAccountInf(transJnl.getCrActNo());
            if (retAccountInf != null) {
                logger.info("账户[" + transJnl.getCrActNo() + "]已存在,返回开户成功");
            } else {
                AccountInf accountInf = new AccountInf();
                accountInf.setActNo(transJnl.getCrActNo());
                accountInf.setUsrId(usrId);
                accountInf.setActTyp(actTyp);
                accountInf.setActSts(MainActSts.MAIN_NORMAL_STS.getCode());
                accountInf.setActStsw(MainActStsW.MAIN_STSW_TTTTT.getCode());
                accountInf.setUpdBalFlg(UpdBalFlg.IS_UPDATE.getCode());
                accountInfMapper.insert(accountInf);
                logger.info("开户流程成功" + usrId);
            }
        }
    }

    public AccountInf queryAccountInf(String actNo) {
        return accountInfMapper.qryAccountInf(actNo);
    }

}
